package com.warehouse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.warehouse.dto.PageResult;
import com.warehouse.entity.ProType;
import com.warehouse.entity.Warehouse;
import com.warehouse.entity.WarehouseExample;
import com.warehouse.mapper.WarehouseMapper;
import com.warehouse.service.Wareservice;
import com.warehouse.utils.IdWorker;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: warehouse
 * @description:
 * @author: xiaojiu
 * @create: 2021-01-14 09:07
 */
@Service
public class WareServiceImpl implements Wareservice {
    @Autowired(required = false)
    private WarehouseMapper warehouseMapper;
    @Autowired(required = false)
    private IdWorker idWorker;
    @Override
    public PageResult<Warehouse> findAllWarehouse(Integer currentPage, Integer pageSize) {
//        WarehouseExample example=new WarehouseExample();
//        example.setOffset((currentpage-1)*pageSize);
//        example.setLimit(pageSize);
        PageHelper.startPage(currentPage, pageSize);
        List<Warehouse> warehouses = warehouseMapper.findwareInfo();
        PageInfo<Warehouse> warehousePageInfo = new PageInfo<>(warehouses);
        return new PageResult<>(warehousePageInfo.getTotal(), warehousePageInfo.getList(), (long) warehousePageInfo.getPages());
    }

    @Override
    public boolean addWarehouse(Warehouse warehouse) {
        String id=idWorker.nextId()+"";
        warehouse.setId(id);
        System.out.println(id);
        int i = warehouseMapper.insertSelective(warehouse);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean editWarehouse(Warehouse warehouse) {
        int i = warehouseMapper.updateByPrimaryKeySelective(warehouse);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delWarehouse(String id) {
        int i = warehouseMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public List<Warehouse> findWarehouse(Warehouse warehouse) {
        List<Warehouse> warehouses=warehouseMapper.findWare(warehouse);
        return warehouses;
    }
}
